﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Dynamic;

namespace CSharp4 {
    public class DemoDynamic {
        public void demoVar() {
            var x = 1;
            Console.WriteLine("x= {0}", x);            
        }
        public void demoDynamicVariable() {
            dynamic x = 1;
            Console.WriteLine("x= {0}", x);
            x = "Hello World";
            Console.WriteLine("x= {0}", x);
        }

        public void demoDynamicVariable01() {
            dynamic foo = new Foo();
            int result = foo.Multiply(10, 10);  //Method does not exit but compile ok            
        }

        private dynamic demoLINQReturnAnonymousClass() {
            Person[] listPerson = new Person[] {
                new Person {Name = "Cuong", Job="Dev", Age=35},
                new Person {Name = "Long", Job="Singer", Age=38},
                new Person {Name = "Trang", Job="Housewife", Age=28}
            };

            return from p in listPerson select new { p.Name, p.Age };
        }

        public void returnDynamic() {
            dynamic result = demoLINQReturnAnonymousClass();
            foreach (var p in result) {
                Console.WriteLine("{0} - {1}", p.Name, p.Age);
            }
        }

        public void testBar() {
            var bar = new Bar { fun = new Foo() };
            Console.WriteLine("{0}", bar.fun.Add(10, 10));

            var personBar = new Bar { fun = new Person { Name = "Cuong", Job = "Dev", Age = 35 } };
            Console.WriteLine("{0} - {1} - {2}", personBar.fun.Name, personBar.fun.Job, personBar.fun.Age);
        }
    }

    public class Foo {
        public int Add(int a, int b) {
            return a + b;
        }
    }

    public class Person {
        public string Name { get; set; }
        public string Job { get; set; }
        public int Age { get; set; }
    }

    public class Bar {
        public dynamic fun { get; set; }
    }    
}
